package com.zzyl.nursing.task;

import com.zzyl.nursing.service.IDeviceDataService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * 设备数据清理定时任务
 */
@Slf4j
@Component
public class DeviceDataCleanTask {
    
    @Autowired
    private IDeviceDataService deviceDataService;
    
    /**
     * 每天凌晨2点执行数据清理任务
     * 删除超过15天的设备数据
     */
    @Scheduled(cron = "0 0 2 * * ?")
    public void cleanExpiredDeviceData() {
        try {
            log.info("开始执行设备数据清理任务，时间: {}", LocalDateTime.now());
            
            int deletedCount = deviceDataService.cleanExpiredDeviceData();
            
            log.info("设备数据清理任务执行完成，删除了 {} 条超过15天的数据", deletedCount);
        } catch (Exception e) {
            log.error("设备数据清理任务执行失败", e);
        }
    }
}
